package com.ideal.manage.esms.service.project;

import com.ideal.manage.esms.bean.project.CategoryManage;
import com.ideal.manage.esms.bean.project.ThresholdManage;
import com.ideal.manage.esms.bean.sys.User;
import com.ideal.manage.esms.config.shiro.MyShiroRealm;
import com.ideal.manage.esms.repository.framework.MySpecification;
import com.ideal.manage.esms.repository.framework.SpecificationOperator;
import com.ideal.manage.esms.repository.project.CategoryManageRepository;
import com.ideal.manage.esms.util.HttpRequests;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class CategoryManageService {
    @Autowired
    private CategoryManageRepository categoryManageRepository;

    @Cacheable(value = "projectPage")
    public Page<CategoryManage> findAllCategory(int pageNum, HttpServletRequest request) {
        List<SpecificationOperator> operators = HttpRequests.getParametersStartingWith(request, "Q_");
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        SpecificationOperator operator = new SpecificationOperator("isValid", "1", "EQ");
        operators.add(operator);
        MySpecification<CategoryManage> mySpecifications = new MySpecification<>(operators);
        Pageable pageable = new PageRequest(pageNum, 15, sort);
        Page<CategoryManage> page = categoryManageRepository.findAll(mySpecifications, pageable);
        return page;
    }

    public void saveCategoryManageInfo(String id, String code, String name, String objName, String useTarget) {
        MyShiroRealm.ShiroUser shiroUser = (MyShiroRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        Date date = new Date();
        CategoryManage categoryManage = new CategoryManage();
        String uuid = "";
        if (id != null) {
            categoryManage = findById(id);
            categoryManage.setUpdateTime(new Date());
            categoryManage.setCreateUser(new User(shiroUser.getId()));
            categoryManage.setIsValid(1);
        } else {
            uuid = UUID.randomUUID().toString().replaceAll("-", "");
            categoryManage.setId(uuid);
            categoryManage.setCreateTime(date);
            categoryManage.setCreateUser(new User(shiroUser.getId()));
            categoryManage.setIsValid(1);
        }

            categoryManage.setCode(code);
            categoryManage.setName(name);
            categoryManage.setObjName(objName);
            categoryManage.setUseTarget(useTarget);

        categoryManageRepository.save(categoryManage);

    }
    //回显数据
    public CategoryManage findById(String id) {
        CategoryManage code = categoryManageRepository.findOne(id);
        return code;
    }
    //参数测点删除
    public void delCategoryManageInfos(String[] ids) {
        List<CategoryManage> controlOrdersInfo = categoryManageRepository.findAll(Arrays.asList(ids));
        for (CategoryManage category:
             controlOrdersInfo) {
            category.setIsValid(0);
        }
        categoryManageRepository.save(controlOrdersInfo);
    }
    //导出
    public List<CategoryManage> findAllCategoryManageInfos() {
        return categoryManageRepository.findByValid();
    }
    //导入数据
    public String saveImport(List<String[]> list,String flag) {
        flag = "0";
        List<CategoryManage> allCode = categoryManageRepository.findCode();
        for (String [] info:list) {
            if(allCode.contains(info[0])){
                return flag = "1";
            }
            CategoryManage oinfo = new CategoryManage();
            oinfo.setCode(info[0]);
            oinfo.setName(info[1]);
            oinfo.setObjName(info[2]);
            oinfo.setUseTarget(info[3]);


            MyShiroRealm.ShiroUser shiroUser = (MyShiroRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            oinfo.setId(uuid);
            oinfo.setCreateTime(new Date());
            oinfo.setCreateUser(new User(shiroUser.getId()));
            oinfo.setIsValid(1);
            categoryManageRepository.save(oinfo);
        }
        return flag;
    }

    /**
     * code 的校验
     * @return
     */
    public List<CategoryManage> findCode() {
        return categoryManageRepository.findCode();
    }
}
